//
//  JSMProvinceDetailsDetailsViewController.m
//  TakeYouTourTogether
//
//  Created by dllo on 15/12/5.
//  Copyright © 2015年 king. All rights reserved.
//

#import "JSMProvinceDetailsDetailsViewController.h"
#import "JSMLeavingDiscoverShowPictureModel.h"

#import "JSMDiscoverShowModel.h"
#import "JSMDetailsDetailsCollectionViewCell.h"
#import "JSMDiscoverDetailsShowTableviewCell.h"
#import "JSMCommentTableViewCell.h"
@interface JSMProvinceDetailsDetailsViewController ()<UITableViewDelegate, UITableViewDataSource, UICollectionViewDataSource, UICollectionViewDelegate>
@property(nonatomic, strong) BaseTableView *tableView;
@property(nonatomic, strong) NSMutableArray *commentMarr;
@property(nonatomic, strong) JSMDiscoverShowModel *showModel;
@property(nonatomic, strong) NSMutableArray *pictureMarr;
@property(nonatomic, strong) BaseCollectionView *collectionView;
@property(nonatomic, strong) BaseLabel *infoLabel;

@end

@implementation JSMProvinceDetailsDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = self.infoModel.name;
    [self createTableView];
    //    [self createCollectionView];
    [self createHeaderView];
    [self createData];

}
- (void)createHeaderView {
    BaseView *view = [[BaseView alloc] initWithFrame:CGRectMake(0, 0, VIEWWIDTH, 180)];
    self.tableView.tableHeaderView = view;
    [self createCollectionView];
    [view addSubview:self.collectionView];
    self.infoLabel = [[BaseLabel alloc] initWithFrame:CGRectMake(15, self.collectionView.frame.size.height - 20, 60, 20)];
    self.infoLabel.textAlignment = NSTextAlignmentCenter;
    self.infoLabel.textColor = [UIColor whiteColor];
    self.infoLabel.layer.cornerRadius = 10;
    self.infoLabel.clipsToBounds = YES;
    self.infoLabel.backgroundColor = [UIColor colorWithRed:0.338 green:0.336 blue:0.339 alpha:0.540];
}

- (void)createCollectionView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(VIEWWIDTH, 180);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, VIEWWIDTH, 180) collectionViewLayout:flowLayout];
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    self.collectionView.pagingEnabled = YES;
    self.collectionView.bounces = NO;
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    [self.collectionView registerClass:[JSMDetailsDetailsCollectionViewCell class] forCellWithReuseIdentifier:@"collection"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.pictureMarr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    JSMDetailsDetailsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collection" forIndexPath:indexPath];
    [cell.bigImageView sd_setImageWithURL:[NSURL URLWithString:[self.pictureMarr[indexPath.row] picture]] placeholderImage:PlaceHolderImage];
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else {
        return self.commentMarr.count;
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15], NSFontAttributeName, nil];
        CGRect rect = [self.showModel.detail boundingRectWithSize:CGSizeMake(VIEWWIDTH - 30, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        return rect.size.height + 10;
    }else {
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15], NSFontAttributeName, nil];
        CGRect rect = [self.commentMarr[indexPath.row][@"count"] boundingRectWithSize:CGSizeMake(6 * VIEWWIDTH / 7 - 30, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        CGFloat cellHeight = VIEWWIDTH / 10;
        if (rect.size.height < cellHeight) {
            return cellHeight  + 10 + cellHeight / 2;
        } else {
            return rect.size.height + 10 + cellHeight / 2;
        }
        
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        static NSString *reuse = @"reuse";
        JSMDiscoverDetailsShowTableviewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[JSMDiscoverDetailsShowTableviewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
        }
        cell.onceLabel.text = self.showModel.detail;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else {
        static NSString *reuse = @"twoReuse";
        JSMCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[JSMCommentTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
        }
        cell.titleLabel.text = self.commentMarr[indexPath.row][@"content"];
        cell.userIfoNameLabel.text = [NSString stringWithFormat:@"%@%@", self.commentMarr[indexPath.row][@"created_by"][@"nickname"], self.commentMarr[indexPath.row][@"trail_title"]];
        [cell.userImageView sd_setImageWithURL:[NSURL URLWithString:self.commentMarr[indexPath.row][@"created_by"][@"avatar"]] placeholderImage:PlaceHolderImage];
        cell.userImageView.layer.borderColor = [[UIColor colorWithRed:1.000 green:0.499 blue:0.566 alpha:1.0] CGColor];
        return cell;
    }
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        BaseView *view = [[BaseView alloc] initWithFrame:CGRectMake(0, 0, VIEWWIDTH, 40)];
        BaseLabel *titleLabel = [[BaseLabel alloc] initWithFrame:CGRectMake(0, 0, VIEWWIDTH, 40)];
        [view addSubview:titleLabel];
        titleLabel.text = [NSString stringWithFormat:@"%@ . %@", self.infoModel.destination, self.infoModel.name];
        return view;
    } else {
        BaseView *view = [[BaseView alloc] initWithFrame:CGRectMake(0, 0, VIEWWIDTH, 40)];
        BaseLabel *titleLanel = [[BaseLabel alloc] initWithFrame:CGRectMake(0, 0, VIEWWIDTH, 40)];
        [view addSubview:titleLanel];
        titleLanel.text = @" 评论";
        titleLanel.textColor = [UIColor orangeColor];
        return view;
    }
}

- (void)createData {
    //    http://tubu.ibuzhai.com/rest/v2/trail/%@?&api_version=1&app_version=4.3.3&trail_id=%@&device_type=2
    
    NSString *url = [NSString stringWithFormat:@"http://tubu.ibuzhai.com/rest/v2/trail/%@?&api_version=1&app_version=4.3.3&trail_id=%@&device_type=2", self.infoModel.detailsId, self.infoModel.detailsId];
    //    NSLog(@"网址%@", url);
    [AFNetworkingTool getUrl:url hudSuperView:self.view result:JSON success:^(id result) {
        if (result != nil) {
            self.showModel = [[JSMDiscoverShowModel alloc] init];
            [self.showModel setValuesForKeysWithDictionary:result];
            self.pictureMarr = [NSMutableArray array];
            self.pictureMarr = self.showModel.picturesMarr;
            [self.tableView reloadData];
            [self.collectionView reloadData];
            //            self.infoLabel = [NSString stringWithFormat:@""]
        }
        
    } failure:^(NSError *error) {
        
    }];
    NSString *commentUrl = [NSString stringWithFormat:@"http://tubu.ibuzhai.com/rest/v1/trail/%@/bbs/1/posts?&page_size=20&page=1&device_type=2&app_version=4.3.3&bbs_catid=1&api_version=1&trail_id=%@&access_token=", self.infoModel.detailsId, self.infoModel.detailsId];
    [AFNetworkingTool getUrl:commentUrl hudSuperView:self.view result:JSON success:^(id result) {
        if (result != nil) {
            //            NSLog(@"111111````````````````````%@", commentUrl);
            self.commentMarr = [NSMutableArray array];
            self.commentMarr = result[@"posts"];
            //            NSLog(@"%@", self.commentMarr);
            [self.tableView reloadData];
        }
    } failure:^(NSError *error){
        
    }];
}
- (void)createTableView {
    self.tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, VIEWWIDTH, VIEWHEIGHT) style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
